cmake_minimum_required(VERSION 2.8)

project(openctp_ctp)

set(CMAKE_CXX_STANDARD 11)

if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
        if(CMAKE_GENERATOR_PLATFORM STREQUAL "x86")
                include_directories(${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/win32 $ENV{PYTHON_INCLUDE})
                link_directories(${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/win32 $ENV{PYTHON_LIB})
        else()
                include_directories(${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/win64 $ENV{PYTHON_INCLUDE})
                link_directories(${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/win64 $ENV{PYTHON_LIB})
        endif()
    add_compile_options(/bigobj /MT)
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
    include_directories(${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/lin64 $ENV{PYTHON_INCLUDE})
    link_directories(${PROJECT_SOURCE_DIR} $ENV{PYTHON_LIB})
endif()

set(tdapi _thosttraderapi)
set(mdapi _thostmduserapi)

message("generate swig wrap files ...")
if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
        if(CMAKE_GENERATOR_PLATFORM STREQUAL "x86")
                execute_process(COMMAND swig -threads -c++ -python -outdir . -outcurrentdir -I${PROJECT_SOURCE_DIR}/win32 ${PROJECT_SOURCE_DIR}/../thosttraderapi.i)
                execute_process(COMMAND swig -threads -c++ -python -outdir . -outcurrentdir -I${PROJECT_SOURCE_DIR}/win32 ${PROJECT_SOURCE_DIR}/../thostmduserapi.i)
        else()
                execute_process(COMMAND swig -threads -c++ -python -outdir . -outcurrentdir -I${PROJECT_SOURCE_DIR}/win64 ${PROJECT_SOURCE_DIR}/../thosttraderapi.i)
                execute_process(COMMAND swig -threads -c++ -python -outdir . -outcurrentdir -I${PROJECT_SOURCE_DIR}/win64 ${PROJECT_SOURCE_DIR}/../thostmduserapi.i)
        endif()
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
        execute_process(COMMAND swig -threads -c++ -python -outdir . -outcurrentdir -I${PROJECT_SOURCE_DIR}/lin64 ${PROJECT_SOURCE_DIR}/../thosttraderapi.i)
        execute_process(COMMAND swig -threads -c++ -python -outdir . -outcurrentdir -I${PROJECT_SOURCE_DIR}/lin64 ${PROJECT_SOURCE_DIR}/../thostmduserapi.i)
endif()

add_library(${tdapi} SHARED thosttraderapi_wrap.cxx)
if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
    set_target_properties(${tdapi} PROPERTIES POSITION_INDEPENDENT_CODE ON)
    set_target_properties(${tdapi} PROPERTIES LINK_FLAGS "-Wl,-Bsymbolic")
    set_property(TARGET ${tdapi} PROPERTY IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/lin64/thosttraderapi_se.so)
    set_target_properties(${tdapi} PROPERTIES PREFIX "")
    target_link_libraries(${tdapi} pthread ${tdapi})
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
    target_link_libraries(${tdapi} thosttraderapi_se)
endif()

add_library(${mdapi} SHARED thostmduserapi_wrap.cxx)
if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
    set_target_properties(${mdapi} PROPERTIES POSITION_INDEPENDENT_CODE ON)
    set_target_properties(${mdapi} PROPERTIES LINK_FLAGS "-Wl,-Bsymbolic")
    set_property(TARGET ${mdapi} PROPERTY IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/lin64/thostmduserapi_se.so)
    set_target_properties(${mdapi} PROPERTIES PREFIX "")
    target_link_libraries(${mdapi} pthread ${mdapi})
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
    target_link_libraries(${mdapi} thostmduserapi_se)
endif()
